/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


var map;
var wg;
var DIV_CONTROL;
var infowindow;
var contenedor, contenedor2;
var form, form2, form3, find_form;
function initialize(divme) {
    console.log("loading");
    var Options = {
        zoom: ZOOM,
        center: new google.maps.LatLng(p_latlng[0], p_latlng[1]),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        panControl: false,
        mapTypeControl: false,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
            position: google.maps.ControlPosition.RIGHT_BOTTOM
        },
        zoomControl: true,
        zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL,
            position: google.maps.ControlPosition.RIGHT_BOTTOM
        },
        scaleControl: false,
        streetViewControl: false
    };
    google.maps.visualRefresh = true;
    
    if (divme != undefined | divme != null) {
        map = new google.maps.Map($("#"+divme)[0], Options);
        console.log("encontrado:"+map);
    } else{
        map = new google.maps.Map($("#map_canvas")[0], Options);
    }

    infowindow = new google.maps.InfoWindow();
    google.maps.event.addListenerOnce(map, 'idle', customHTMLmap);
    wg = new Wg(JS);

//CONTENEDOR + FORMULARIO BASE
    contenedor = new Conten('ID_FORM');
    form = new Form({
        id: 'Form1',
        title: NO_CONGEO,
        visible: true,
        top: 10,
        hidden: true
    });
    $.each(wg.ls_concap, function(i, js_capa) {
        var capa = new Item({
            co_item: form.id + "" + js_capa.co_concap,
            no_item: js_capa.no_concap,
//            image: 'img/geo/layer.png',
            image: '../img/geo/layer.png',
            enabled: js_capa.il_visible
        });
        capa.setAction(fentida(js_capa));
        capa.setextraAction(fecapa(capa, i));
        form.addElement(capa);
    });

    form2 = new Form({
        id: 'Form2',
        title: NO_CONGEO,
        visible: false,
        top: 10
    });
    form2.setAction(to_capas);

    form3 = new Form({
        id: 'Form3',
        title: NO_CONGEO,
        visible: false,
        top: 10
    });
    form3.setAction(to_entida);

    contenedor.addForm(form);
    contenedor.addForm(form2);
    contenedor.addForm(form3);

    map.controls[google.maps.ControlPosition.LEFT_TOP].push(contenedor.render());

}

function customHTMLmap() {
    try {
        $('#map_canvas').find('a')[0].href = 'http://workflow.accesocrediticio.com';
        $('#map_canvas').find('a div')[0].innerHTML = 'EDPYME ACCESO CREDITICIO';
        $('#map_canvas').find('a div')[0].style.width = '170px';
        $('#map_canvas').find('a div')[0].style.color = 'WHITE';
        $('#map_canvas').find('a div')[0].style.fontWeight = 'bold';
        $('#map_canvas').find('a div')[0].style.fontSize = '12px';
        $('#map_canvas').find('.gm-style .gm-style-cc')[0].style.display = "none";
        $('#map_canvas').find('.gm-style .gm-style-cc')[1].style.display = "none";
        $('#map_canvas').find('.gm-style .gm-style-cc')[2].style.display = "none";
    } catch (e) {
        setTimeout(customHTMLmap, 100);
    }
}

function fentida(js_capa) {
    return function() {
        viewEntida({ls_entgeo: js_capa.ls_entgeo, no_concap: js_capa.no_concap});
    };
}

function fgrafo(js_entida) {
    return function() {
        viewGrafo({ls_grafos: js_entida.ls_grafos, no_entgeo: js_entida.no_entgeo});
    };
}

//                function fecapa(id) {
function fecapa(capa, id) {
    return function() {
        wg.getConcap(id).setVisible(!wg.getConcap(id).isVisible());
        capa.setEnabled(wg.getConcap(id).isVisible());
    };
}

function feentida(entida) {
    return function() {
        entida.setVisible(!entida.isVisible());
//        entidad.setEnabled(entida.isVisible());
    };
}

function viewEntida(g) {
    form2.clearAllElements();
    $.each(g.ls_entgeo, function(i, js_entida) {
        var entidad = new Item({
            co_item: form2.id + "" + js_entida.co_entgeo,
            no_item: js_entida.no_entgeo,
            enabled: g.ls_entgeo[i].isVisible(),
        });
        entidad.setAction(fgrafo(js_entida));
//        entidad.setextraAction(feentida(g.ls_entgeo[i]));
        entidad.setextraAction(feentida(g.ls_entgeo[i]));
        switch (js_entida.ti_entgeo) {
            case  'L':
//                entidad.setImage('img/geo/line.png');
                entidad.setImage('../img/geo/line.png');
                break;
            case  'P':
//                entidad.setImage('img/geo/point.png');
                entidad.setImage('../img/geo/point.png');
                break;
            case  'C':
//                entidad.setImage('img/geo/circle.jpeg');
                entidad.setImage('../img/geo/circle.jpeg');
                break;
            case  'A':
//                entidad.setImage('img/geo/area.png');
                entidad.setImage('../img/geo/area.png');
                break;
        }
        form2.addElement(entidad);
    });
    form.setVisible(false);
//    form2.setTitle(g.no_concap);
    form2.setSub_title(g.no_concap);
    form2.setVisible(true);
}

function viewGrafo(g) {
    form3.clearAllElements();
    $.each(g.ls_grafos, function(i, grafo) {
        var entidad = new Item({
            co_item: form3.id + "" + g.ls_grafos[i].id + "_" + (i + 1),
            no_item: grafo.no_entnom != null ? grafo.no_entnom : 'Grafo ' + (i + 1),
            no_subitem: grafo.no_entdes});
        entidad.setAction(fegrafo(grafo));
        entidad.setMouseOverAction(fegrafo_over(grafo));
        entidad.setMouseOutAction(fegrafo_out(grafo));
        form3.addElement(entidad);
    });
    form2.setVisible(false);
    form3.setSub_title(form2.getSub_title() + " > " + g.no_entgeo);
    form3.setVisible(true);
}

function fegrafo(grafo) {
    return function() {
        grafo.click_me();
    };
}
function fegrafo_over(grafo) {
    return function() {
        grafo.in_me();
    };
}
function fegrafo_out(grafo) {
    return function() {
        grafo.out_me();
    };
}

function to_capas() {
    form2.setVisible(false);
    form.setVisible(true);
}
function to_entida() {
    form3.setVisible(false);
    form2.setVisible(true);
}



function infocredito(event) {
    infowindow.setContent(this.descripcion);
//                infowindow.setPosition(event.latLng);
    try {
        infowindow.setPosition(event.latLng);
//                    console.log(">>abriendo infowindow: " + event.latLng+"::"+this.descripcion);
    } catch (e) {
        infowindow.setPosition(event.getCenter());
//                    console.log("abriendo infowindow: " + event.latLng);
    }
    infowindow.open(map);
    //extra-> ir a seleccion

}

function infocredito2(grafo) {
    infowindow.setContent(grafo.descripcion);
    try {
        infowindow.setPosition(grafo.getPosition());
        map.panTo(grafo.getPosition());
    } catch (e) {
        console.log("e:" + e);
        infowindow.setPosition(grafo.getCenter());
        map.panTo(grafo.getCenter());
    }
    infowindow.open(map);
}

function pmouseover(event) {
//                console.log("IN:"+this.type);
    if (this.type < 6) {
        this.setOptions({
            strokeOpacity: 1,
            strokeWeight: 3,
            icons: create_icon(this.type, 1)
        });
    } else {
        this.setOptions({
            strokeWeight: 3,
            icons: create_icon(this.type, 1)
        });
    }
}

function pmouseout(event) {
//                console.log("out:"+this.type);
    if (this.type < 6) {
        this.setOptions({
            strokeOpacity: 0.7,
            strokeWeight: 2,
            icons: create_icon(this.type, 0.7)
        });
    } else {
        this.setOptions({
            strokeWeight: 2,
            icons: create_icon(this.type, 0.7)
        });
    }

}

function pmouseover2(grafo) {
    try {
        map.panTo(grafo.getCenter());
    } catch (e) {
        map.panTo(grafo.getPosition());
    }

    if (grafo.type < 6) {
        grafo.setOptions({
            strokeOpacity: 1,
            strokeWeight: 3,
            icons: create_icon(grafo.type, 1)
        });
    } else {
        grafo.setOptions({
            strokeWeight: 3,
            icons: create_icon(grafo.type, 1)
        });
    }
}

function pmouseout2(grafo) {
//                console.log("out2");
    if (grafo.type < 6) {
        grafo.setOptions({
            strokeOpacity: 0.7,
            strokeWeight: 2,
            icons: create_icon(grafo.type, 0.7)
        });
    } else {
        grafo.setOptions({
            strokeWeight: 2,
            icons: create_icon(grafo.type, 0.7)
        });
    }

}

function create_icon(ti_entgra, p_stroke) {
    var type;
    switch (ti_entgra) {
        case 1:
            {
                type = null;
                break;
            }
        case 2:
            {
                type = [{
                        icon: {
                            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '100%'
                    }];
                break;
            }
        case 3:
            {
                type = [{
                        icon: {
                            path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '0%'
                    }];
                break;
            }
        case 4:
            {
                type = [{
                        icon: {path: google.maps.SymbolPath.CIRCLE,
                            strokeOpacity: p_stroke},
                        offset: '0px'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '100%'
                    }];
                break;
            }
        case 5:
            {
                type = [{
                        icon: {path: google.maps.SymbolPath.CIRCLE,
                            strokeOpacity: p_stroke},
                        offset: '100%'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '0%'
                    }];
                break;
            }
        case 6:
            {
                type = [{
                        icon: {
                            path: 'M 0,-1 0,1',
                            strokeOpacity: p_stroke,
                            scale: 4
                        },
                        offset: '20',
                        repeat: '20px'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '100%'
                    }];
                break;
            }
        case 7:
            {
                type = [{
                        icon: {
                            path: 'M 0,-1 0,1',
                            strokeOpacity: p_stroke,
                            scale: 4
                        },
                        offset: '0',
                        repeat: '20px'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke
                        },
                        offset: '0%'
                    }];
                break;
            }
        case 8:
            {
                type = [{
                        icon: {path: google.maps.SymbolPath.CIRCLE,
                            strokeOpacity: p_stroke},
                        offset: '0px'
                    }, {
                        icon: {
                            path: 'M 0,-1 0,1',
                            strokeOpacity: p_stroke,
                            scale: 4
                        },
                        offset: '20',
                        repeat: '20px'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke,
                        },
                        offset: '100%'
                    }];
                break;
            }
        case 9:
            {
                type = [{
                        icon: {path: google.maps.SymbolPath.CIRCLE,
                            strokeOpacity: p_stroke},
                        offset: '100%',
                    }, {
                        icon: {
                            path: 'M 0,-1 0,1',
                            strokeOpacity: p_stroke,
                            scale: 4
                        },
                        offset: '0',
                        repeat: '20px'
                    }, {
                        icon: {
                            path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
                            strokeOpacity: p_stroke,
                        },
                        offset: '0%'
                    }];
                break;
            }
    }
    return type;
}
